unit Unit49; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, sqldb,
  DbCtrls, StdCtrls, db, Unit1;

type

  { TForm49 }

  TForm49 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Datasource1: TDatasource;
    DBComboBox1: TDBComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    SQLQuery1: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow();
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  Form49: TForm49; 

implementation

{ TForm49 }

procedure TForm49.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
end;

procedure TForm49.FormShow();
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;
    
  SQLQuery1.SQL.Text := 'select fnc_nombre from funcionario order by fnc_nombre';

  SQLQuery1.Open;

  DBComboBox1.DataField := 'fnc_nombre';
  DBComboBox1.Clear;

  while not SQLQuery1.EOF do
  begin
    DBComboBox1.Items.Add(SQLQuery1.Fields[0].AsString);
    SQLQuery1.Next;
  end;

  DBComboBox1.Text := '';
  Edit1.Text := '';
  EDit2.Text := '';
  
  DBComboBox1.SetFocus;
end;

procedure TForm49.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm49.Button1Click(Sender: TObject);
var
  QueryString : String;
  
begin
  QueryString := 'insert into retiro_caja(rtc_funcionario, rtc_descripcion, rtc_monto, rtc_sucursal) '
    + 'values((select fnc_cdg from funcionario where fnc_nombre = ''' + DBComboBox1.Text + '''), '''
    + Edit1.Text + ''', ' + Edit2.Text + ', ' + registro.codigo_sucursal + ')';
    
  Form1.PQConnection1.ExecuteDirect('begin work;');
  Form1.PQConnection1.ExecuteDirect(QueryString);
  Form1.PQConnection1.ExecuteDirect('commit work;');
  
  ShowMessage('ALTA EXITOSA');
  
  FormShow;
end;

procedure TForm49.Button2Click(Sender: TObject);
begin
  Close;
end;

initialization
  {$I unit49.lrs}

end.

